local gamelib = require "club.threecard.gamelib"
local blaze = require "blaze"

local ingame = {}

local log = gamelib.logging.get("ingame store")

local phaseConfig = 
{
    [0] = "进入游戏房间";
    [103] = "空闲中";
    [104] = "发牌阶段";
    [105] = "看牌阶段";
    [106] = "换牌阶段";
    [107] = "最后下注阶段";
    [108] = "展示阶段";
    [109] = "结算阶段";
}

local betConfig = 
{
    [1] = 1;
    [2] = 2;
    [3] = 5;
    [4] = 10;
    [5] = 20;
}

ingame.state = 
{
    phase = 0;
    left_time = 0;
    selected_bet_index = 1;
    bet_amount = 0;
    statistics = 
    {
        total_player_count = 0;
        seen_player_count = 0;
        exchange_player_count = 0;
        total_bets = 0;
        top_bets = {};
    };
    ope_status = 
    {
        can_bet = false;
        can_see = false;
        can_exchange = false;
    },
    settlement = nil;
}

ingame.actions = 
{
    get_phase_name = function(store, state)
        return phaseConfig[state.phase];
    end,
    get_bet_config = function(store, state)
        return betConfig;
    end
}

local started = false;
local function startTimer(store, state)
    if started then
        return;
    end

    gamelib.coroutine.start(function()
        while true do
            gamelib.coroutine.wait(1);
            state.left_time = state.left_time - 1;

            store.emit("on_left_time_change");
        end
    end);

    started = true;
end

ingame.mutations = 
{
    on_phase = function(store, state, args)
        state.phase = args.phaseId;
        state.left_time = args.countdown;

        store.emit("on_phase");
        startTimer(store, state);
    end,

    on_statistics = function(store, state, args)
        local original = state.statistics.top_bets;
        state.statistics = args.info;
        
        for i = 1, #original do
            table.remove(original);
        end

        for _, bet in ipairs(args.info.top_bets) do
            table.insert(original, bet);
        end
        state.statistics.top_bets = original;
        store.emit("on_statistics");
    end,

    on_ope_status = function(store, state, args)
        state.ope_status.can_bet = args.can_bet == 1;
        state.ope_status.can_see = args.can_see == 1;
        state.ope_status.can_exchange = args.can_exchange == 1;

        store.emit("on_ope_status", state.ope_status);
    end,

    on_change_bet = function(store, state, args) 
        local value = state.selected_bet_index + args.changed;
        if value > #betConfig then
            value = 1;
        end
        if value < 1 then
            value = #betConfig;
        end

        state.selected_bet_index = value;

        store.emit("on_change_bet");
    end,
    
    on_change_chip = function(store, state, args)
        store.emit("on_change_chip", args);
    end,

    on_change_bet_amount = function(store, state, args)
        state.bet_amount = args.bet_amount;
        store.emit("on_change_bet_amount", args);
    end,

    on_deal = function(store, state, args)
        store.emit("发牌", args);
    end,

    on_see = function(store, state, args) 
        store.emit("看牌", args);
    end,

    on_exchange = function(store, state, args)
        store.emit("换牌", args);
    end,

    on_settlement = function(store, state, args)
        state.settlement = args.info;
        store.emit("结算");
    end
}

return ingame